﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Pavilion;
using Pavilion.Control;

namespace PavilionGame
{
    public class NarrativeView : IView
    {
        private PauseMenu pauseMenu;
        private GameSession gameSession;

        public NarrativeView()
        {
            RootControl.Instance.Keyboard.KeysPressed += new Action<Keys[]>(Keyboard_KeyPressed);
        }

        private void InitializeControls()
        {
            int x = GameEngine.WindowWidth / 5;
            int y = GameEngine.WindowHeight / 5;

            pauseMenu = new PauseMenu(new Rectangle(x, y, GameEngine.WindowWidth - x - x, GameEngine.WindowHeight - y - y));

            pauseMenu.QuitPressed += new Action(pauseMenu_QuitPressed);
            pauseMenu.Exit += new Action(ShowStage);
        }

        public void Load(GameSession gameSession)
        {
            this.gameSession = gameSession;
            InitializeControls();

            pauseMenu.Visible = false;

            RootControl.Instance.Collection.Add(pauseMenu);
        }

        public void Unload()
        {
            pauseMenu.QuitPressed -= new Action(pauseMenu_QuitPressed);
            pauseMenu.Exit -= new Action(ShowStage);

            pauseMenu.Dispose();

            RootControl.Instance.Clear();
        }

        public void Update(GameTime gameTime)
        {
            RootControl.Instance.Update(gameTime);
        }

        public void Draw()
        {
            RootControl.Instance.Draw();
        }

        private void ToggleEnabledForGameItems(bool isEnabled)
        {
            if (gameSession != null)
                gameSession.IsEnabled = isEnabled;
        }

        private void ShowStage()
        {
            pauseMenu.Visible = false;
            ToggleEnabledForGameItems(true);
        }

        private void ShowPauseMenu()
        {
            pauseMenu.Visible = true;
            ToggleEnabledForGameItems(false);
        }

        private void pauseMenu_QuitPressed()
        {
            GameSession.RequestSessionLoad(this, "Home");
        }

        private void Keyboard_KeyPressed(Keys[] keys)
        {
            if (keys.Contains(Keys.Escape))
                ShowPauseMenu();
        }
    }
}
